<?php

namespace app\api\controller;

use app\common\logic\CartLogic;
use app\common\logic\MessageLogic;
use app\common\logic\UsersLogic;
use app\common\logic\OrderLogic;
use app\common\logic\CouponLogic;
use think\Page;
use think\Request;
use think\Verify;
use think\Db;
use think\Session;
use think\Loader;
class Custom extends ApiCommon
{
	
	/*
    * 初始化操作
    */
    public function _initialize()
    {
//      parent::_initialize();
//      if (session('?user')) {
//          $user = session('user');
//          $user = M('users')->where("user_id", $user['user_id'])->find();
//          session('user', $user);  //覆盖session 中的 user
//          $this->user = $user;
//          $this->user_id = $user['user_id'];
////          $this->assign('user', $user); //存储用户信息
//      }
    }
    
	//获得定制的模板
	public function getTemplateList(){		
		$page = I('page/d',0);
		$pagesize = C('PAGESIZE');
		$startpage = $pagesize*$page;
		$res = M('custom')->order('id desc')->limit("$startpage,$pagesize")->select();		
		$this->returnMsg('ok','',$res);
	}
	
	//模板详情
	public function getTemplateMsg(){
		$cid = I('cid/d',0);
		$res = M('custom')->where(['id'=>$cid])->select();
		$att = M('custom_att')->where(['customid'=>$cid])->select();
		$this->returnMsg('ok','',['info'=>$res,'att'=>$att]);
	}
	
	
	//获取用户定制模板信息
	public function getUserCustom(){
		$page = I('page/d',0);
		$pagesize = C('PAGESIZE');
		$startpage = $pagesize*$page;
		$uid = I('uid');
		$uList = Db::view('tp_user_custom','*')
	    ->view('tp_custom','id,name','tp_user_custom.coutomid=tp_custom.id')
	    ->where(['userid'=>$uid])
		->order('tuid desc')->limit($startpage.','.$pagesize)
	    ->select();
		
		$statemsg = C('CUSTOMSTATE');
		$this->returnMsg('ok','',['arr'=>$uList,'sarr'=>$statemsg]);
	}
	
	//获得用户定制信息
	public function getUserCustomMsg(){
		$userid = I('uid/d',0);
		$tuid = I('tuid/d',0);
		$info = M("user_custom")->where(['tuid'=>$tuid,'userid'=>$userid])->find();
		if(empty($info)){
			$this->returnMsg('ok','',['info'=>[],'att'=>[],'smsg'=>[]]);
		}
		$lists = M("user_custom_att")->where(['cid'=>$tuid])->select();
		$statemsg = C('CUSTOMSTATE');
		$this->returnMsg('ok','',['info'=>$info,'att'=>$lists,'smsg'=>$statemsg]);
	}
	
	//添加用户的定制模板
	public function addCustom(){
		$title = I('title');
		$email = I('emaill');
		$phone = I('phone');
		$uid = I('uid');
		$uname = I('uname');
		$coutomid = I('coutomid');
		//检测
		$cvalidate = Loader::validate('Custom');	
		$iDate = ['title'=>$title,'emaill'=>$email,'phone'=>$phone,'userid'=>$uid];
		if (!$cvalidate->batch()->check($iDate)) {
            $this->returnMsg('-1','',$cvalidate->getError());
    	}
		
		$iDate['addtime'] = time();
		$iDate['coutomid'] = $coutomid;
		$iDate['uname'] = $uname;
		$iDate['state'] = 1;
		$lastid = Db::name('user_custom')->add($iDate);
		if($lastid<=0){
			$this->returnMsg('-1','添加失败');
		}
		
		$customatt = I('att/a');
		if ($customatt) {
            foreach ($customatt as $k => $v){
            	$adddate = ['cid'=>$lastid,'attname'=>$v['attname'],'attvalue'=>$v['attvalue'],'tp'=>$v['atttype']];
            	Db::name('user_custom_att')->insert($adddate);
            }
		}
		
		$this->returnMsg('ok','');
	}
	
	
	//上传图片
	public function return_goods_upload(){
		$imgtop = 'public/uploads/custom/';		
		$this->image_upload($imgtop);
    }
	
	public function image_upload($imgtop){
		ob_end_clean();
        $file = request()->file('file');
		
        $info = $file->move(ROOT_PATH . $imgtop);
        if($info){
            $imageurl = $info->getSaveName();			
			$imageurl = preg_replace('/\\\\/', '/', $imageurl);		
			
			$allurls = $imgtop.$imageurl;
				
            $this->returnMsg('ok','',$allurls);
        }
        else{
            $this->returnMsg('-1',$file->getError());
        }
    }
}
?>